package Main;

import java.util.Random;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
/*
 * CCanvas.java
 *
 * Created on Ngï¿½y 20 thï¿½ng 6 nï¿½m 2011, 01:37
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

import Common.CRes;
import Common.Sound;
import Common.Util;
import Gui.GameScr;
import Gui.GameSreen;

/**
 *
 * @author Tuan Pham
 */
public class CCanvas extends Canvas implements Runnable{
	

    
	public static final byte LEFT=4;
	public static final byte RIGHT=6;
	public static final byte UP=2;
	public static final byte DOWN=8;
	public static final byte ENTER=8;
	
	public static Random r = new Random();
	public static boolean isRunning = true;
	public static int delay = 40;
	public static CCanvas instance;
	public static GameScr gameScr;
	public static CScreen curScr;
	
	Thread thread;
	static boolean[] keyHold = new boolean[14];
	public static boolean[] keyPressed = new boolean[14];
	public static boolean isPointerDown;
	public static boolean isPointerClick;
	public static boolean isPointerJustRelease;
	public static boolean isTouch = false; // ---> MÃƒÂ n hÃƒÂ¬nh cÃ¡ÂºÂ£m Ã¡Â»Â©ng
	public static boolean isTouchControl; // ----> Ã„Â�iÃ¡Â»Â�u khiÃ¡Â»Æ’n bÃ¡ÂºÂ±ng cÃ¡ÂºÂ£m Ã¡Â»Â©ng
	public static boolean isTouchControlSmallScreen; // ----> Ã„Â�iÃ¡Â»Â�u khiÃ¡Â»Æ’n bÃ¡ÂºÂ±ng cÃ¡ÂºÂ£m Ã¡Â»Â©ng mÃƒÂ n hÃƒÂ¬nh nhÃ¡Â»Â�
	public static boolean isTouchControlLargeScreen; // ----> Ã„Â�iÃ¡Â»Â�u khiÃ¡Â»Æ’n bÃ¡ÂºÂ±ng cÃ¡ÂºÂ£m Ã¡Â»Â©ng mÃƒÂ n hÃƒÂ¬nh lÃ¡Â»â€ºn
	public static boolean isPointerJustDown;
	public static int px, py, pxFirst, pyFirst, pxLast, pyLast;
	public static int w, h, hw, hh, wd3, hd3, w2d3, h2d3, w3d4, h3d4, wd6, hd6;
	public CCanvas(){
		
		setFullScreenMode(true);
		CScreen.w= getWidth();
		CScreen.h = getHeight();
		CScreen.hw = getWidth() / 2;
		CScreen.hh = getHeight() / 2;

		instance = this;
		
		gameScr = new GameScr();
		gameScr.show();
		
	}
	public void mapKeyPress(int keyCode) {

		switch (keyCode) {
		case -1:
			keyHold[2] = true;
			keyPressed[2] = true;
			return;
		case -2:
			keyHold[8] = true;
			keyPressed[8] = true;
			return;
		case -3:
			keyHold[4] = true;
			keyPressed[4] = true;
			return;
		case -4:
			keyHold[6] = true;
			keyPressed[6] = true;
			return;
		case -5:
		case 10:
			keyHold[5] = true;
			keyPressed[5] = true;
			return; // [i]
		case 48:
		case 49:
		case 50:
		case 51:
		case 52:
		case 53:
		case 54:
		case 55:
		case 56:
		case 57:
			
			keyHold[keyCode - 48] = true;
			keyPressed[keyCode - 48] = true;
		
			return; // 0 -> 9
		case 42:
			keyHold[10] = true;
			keyPressed[10] = true;
			return; // Key [*]
		case 35:
			keyHold[11] = true;
			keyPressed[11] = true;
			return; // Key [#]
		case -6:
		case -21:
			keyHold[12] = true;
			keyPressed[12] = true;
			return; // Soft1
		case -7:
		case -22:
			keyHold[13] = true;
			keyPressed[13] = true;
			return; // Soft2
		}

	}
	//xÃ¡Â»Â­ lÃƒÂ½ sÃ¡Â»Â± kiÃ¡Â»â€¡n phim duy chuyÃ¡Â»â€¦n trong game
	public void mapKeyRelease(int keyCode) {
		switch (keyCode) {
		case -1:
			keyHold[2] = false;
			keyPressed[2] = false;
			return;
		case -2:
			keyHold[8] = false;
			keyPressed[8] = false;
			return;
		case -3:
			keyHold[4] = false;
			keyPressed[4] = false;
			return;
		case -4:
			keyHold[6] = false;
			keyPressed[6] = false;
			return;
		case -5:
		case 10:
			keyHold[5] = false;
			return; // [i]
		case 48:
		case 49:
		case 50:
		case 51:
		case 52:
		case 53:
		case 54:
		case 55:
		case 56:
		case 57:
			keyHold[keyCode - 48] = false;
			return; // 0 -> 9
		case 42:
			keyHold[10] = false;
			return; // Key [*]
		case 35:
			keyHold[11] = false;
			return; // Key [#]
		case -6:
		case -21:
			keyHold[12] = false;
			return; // Soft1
		case -7:
		case -22:
			keyHold[13] = false;
			return; // Soft2
		}
	}
	//xÃ¡Â»Â­ lÃƒÂ½ Ã¡ÂºÂ¥n thÃ¡ÂºÂ£
	public void keyPressed(int keyCode) {

		mapKeyPress(keyCode);
	}

	protected void keyReleased(int keyCode) {

		mapKeyRelease(keyCode);
	}
	
	static void clearKeyHold() {
		for (int i = 0; i < 14; i++)
			keyHold[i] = false;
	}

	public static void clearKeyPressed() {
		for (int i = 0; i < 14; i++)
			keyPressed[i] = false;
	}
	
	
	public void start() {
		thread = new Thread(this);
		thread.start();
	}

	public void run() {
		// TODO Auto-generated method stub
		if (!isRunning) {
			isRunning = true;

		}
		try {
			while (isRunning) {

				long l1 = System.currentTimeMillis();
				input();
				update();
				repaint();
				serviceRepaints();

				while (System.currentTimeMillis() - l1 < delay)
					Thread.yield();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		// Canvas.saveGame();
		Midlet.instance.notifyDestroyed();
		
	}
	
	public static Image loadImage(String fileName) {
		Image img = null;
		try {
			img = Image.createImage(fileName);
		} catch (Exception e) {
		}
		if (img == null)
			System.out.println("Load image '" + fileName + "' :ERORR ");
		return img;
	}

	public static int random(int a, int b) {
		return a + (r.nextInt(b - a));
	}

	public static boolean testToDiffer(int x, int y, int w, int h, int x1,
			int y1, int w1, int h1) {
		return (x + w >= x1 && x <= x1 + w1 && y + h >= y1 && y <= y1 + h1);
	}

	public static final int abs(int x) {
		return x > 0 ? x : -x;
	}

	public static boolean isPointer(int x, int y, int w, int h) {
		if (!isPointerDown && !isPointerJustRelease)
			return false;
		if (px >= x && px <= x + w && py >= y && py <= y + h)
			return true;
		return false;
	}
	
	public static void clearPoint() {
		isPointerDown = isPointerClick = false;
	}

	protected void pointerDragged(int x, int y) {
		px = x;
		py = y;
	}

	protected void pointerPressed(int x, int y) {
		isPointerDown = true;
		px = x;
		py = y;

	}

	protected void pointerReleased(int x, int y) {
		isPointerDown = false;
		isPointerClick = true;
		px = x;
		py = y;
	}

	protected void input() {
		curScr.input();
	}

	protected void update() {
		curScr.update();
	}

	protected void paint(Graphics g) {
		// TODO Auto-generated method stub
		curScr.paint(g);

		
	}

}


